<template>
  <view class="page-body">
    <view class="statistic" v-if="userInfo.nickname == '超级管理员'">
      <view
        class="statistic-box"
        v-for="(item, index) in tabArr"
        :key="index"
        @click="tabClick(index)"
      >
        <view class="statistic-box-title">{{ item.label }}</view>
        <view class="statistic-box-amount">{{ item.amount }}</view>
      </view>
    </view>
    <view class="statistic">
      <view
        class="statistic-box"
        v-for="(item, index) in tabGylArr"
        :key="index"
        @click="tabGylClick(index)"
      >
        <view class="statistic-box-title">{{ item.label }}</view>
        <view class="statistic-box-amount">{{ item.amount }}</view>
      </view>
    </view>
  </view>
</template>

<script>
var that; // 当前页面对象
var vk = uni.vk; // vk实例

export default {
  data() {
    // 页面数据变量
    return {
      tabArr: [
        {
          label: "今日注册",
          amount: 0,
        },
        {
          label: "所有注册",
          amount: 0,
        },
        {
          label: "今日流水",
          amount: 0,
        },
        {
          label: "所有流水",
          amount: 0,
        },
      ],
      tabGylArr: [
        {
          label: "一级分类",
          amount: 0,
        },
        {
          label: "二级分类",
          amount: 0,
        },
        {
          label: "所有商品",
          amount: 0,
        },
        {
          label: "新增商品",
          amount: 0,
        },
      ],
	    userInfo: {}
    };
  },
  // 监听 - 页面每次【加载时】执行(如：前进)
  onLoad(options = {}) {
    that = this;
    vk = that.vk;
  },
  // 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快，会在页面进入动画完成前触发
  onReady() {},
  // 监听 - 页面每次【显示时】执行(如：前进和返回) (页面每次出现在屏幕上都触发，包括从下级页面点返回露出当前页面)
  onShow() {
    this.userInfo = vk.getVuex("$user.userInfo")
    this.init()
  },
  // 监听 - 页面每次【隐藏时】执行(如：返回)
  onHide() {},
  // 函数
  methods: {
    async init() {
      // 今日注册
      // 今日流水
      const { todayStart, todayEnd } = vk.pubfn.getCommonTime(new Date());
      if (this.userInfo.nickname == '超级管理员') {

        vk.callFunction({
          title: "加载中...",
          url: "user/kh/userList",
          data: {
            todayStart,
            todayEnd,
          },
          success: (res) => {
            this.tabArr[0].amount = res.total;
          },
        });
        vk.callFunction({
          title: "加载中...",
          url: "user/kh/userAllget",
          success: (res) => {
            this.tabArr[1].amount = res.total;
          },
        });
        vk.callFunction({
          title: "加载中...",
          url: "user/kh/orderList",
          data: {
            todayStart,
            todayEnd,
          },
          success: (res) => {
            let amount = 0;
            if (res.rows.length > 0) {
              res.rows.map((item) => {
                amount += parseInt(item.price);
              });
              this.tabArr[2].amount = amount;
            }
          },
        });
        vk.callFunction({
          title: "加载中...",
          url: "user/kh/orderAllget",
          success: (res) => {
            let amount = 0;
            if (res.rows.length > 0) {
              res.rows.map((item) => {
                amount += parseInt(item.price);
              });
              this.tabArr[3].amount = amount;
            }
          },
        });
      }
      let info1 = await vk.callFunction({
          title: "加载中...",
          url: "user/kh/cateAllget",
          data: {type: 1}
        });
      this.tabGylArr[0].amount = info1.total;
      let info2 = await vk.callFunction({
          title: "加载中...",
          url: "user/kh/cateAllget",
          data: {type: 2}
        });
      this.tabGylArr[1].amount = info2.total;
      let info3 = await vk.callFunction({
          title: "加载中...",
          url: "user/kh/chainAllget",
        });
      this.tabGylArr[2].amount = info3.total;
      let info4 = await vk.callFunction({
          title: "加载中...",
          url: "user/kh/chainList",
          data: {
            todayStart,
            todayEnd,
          },
        });
      this.tabGylArr[3].amount = info4.total;
    },
	tabClick(index) {
		if (index == 0 || index == 1) {
			vk.navigateTo('/pages_plugs/system/user/list');
		}
		if (index == 2 || index == 3) {
			vk.navigateTo('/pages/order/order');
		}
	},
  tabGylClick(index) {
		if (index == 0 || index == 1) {
			vk.navigateTo('/pages/cate/cate');
		}
		if (index == 2 || index == 3) {
			vk.navigateTo('/pages/good/good');
		}
	},
    pageTo(path) {
      vk.navigateTo(path);
    },
    load(e) {
      console.log(1, e);
    },
    error(e) {
      console.log(2, e);
    },
  },
  // 过滤器
  filters: {},
  // 计算属性
  computed: {},
};
</script>
<style lang="scss" scoped>
.statistic {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  &-box {
    width: 22%;
    height: 100px;
    border-radius: 8rpx;
    border: 1px solid #d0d0d0;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    margin-top: 10rpx;
    cursor: pointer;
	
    &-title {
      font-size: 20px;
    }
    &-amount {
      font-size: 30px;
      font-weight: 500;
      color: #005ecd;
    }
  }
}

</style>
